#include "StdAfx.h"
#include "Regedit.h"


Regedit::Regedit(HKEY hKey)
{
	m_hKey = hKey;
}


Regedit::~Regedit(void)
{
	Close();
}

int Regedit::CreateKey(LPCTSTR lpKeyName, LPCTSTR lpVal, int len)
{
	int nRtn = ERROR_SUCCESS;
	DWORD dwOper;
	HKEY hKey;
	nRtn = RegCreateKeyEx(m_hKey, lpKeyName, 0, NULL, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &hKey, &dwOper);
	if(nRtn != ERROR_SUCCESS)
	{
		return nRtn;
	}
	nRtn = RegSetValueEx(hKey, NULL, 0, REG_SZ, (BYTE *)lpVal, len);

	RegCloseKey(hKey);
	return nRtn;
}

int Regedit::CreateKey(LPCTSTR lpKeyName, DWORD dwVal)
{
	int nRtn = ERROR_SUCCESS;
	DWORD dwOper;
	HKEY hKey;
	nRtn = RegCreateKeyEx(m_hKey, lpKeyName, 0, NULL, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &hKey, &dwOper);
	if(nRtn != ERROR_SUCCESS)
	{
		return nRtn;
	}
	nRtn = RegSetValueEx(hKey, NULL, 0, REG_DWORD, (BYTE *)&dwVal, sizeof(dwVal));

	RegCloseKey(hKey);
	return nRtn;
}

int Regedit::CreateKey(LPCTSTR lpKeyName, LPCTSTR subKey, LPCTSTR lpVal, int len)
{
	int nRtn = ERROR_SUCCESS;
	DWORD dwOper;
	HKEY hKey;
	nRtn = RegCreateKeyEx(m_hKey, lpKeyName, 0, NULL, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &hKey, &dwOper);
	if(nRtn != ERROR_SUCCESS)
	{
		return nRtn;
	}
	nRtn = RegSetValueEx(hKey, subKey, 0, REG_SZ, (BYTE *)lpVal, len);

	RegCloseKey(hKey);
	return nRtn;
}

int Regedit::CreateKey(LPCTSTR lpKeyName, LPCTSTR subKey, DWORD dwVal)
{
	int nRtn = ERROR_SUCCESS;
	DWORD dwOper;
	HKEY hKey;
	nRtn = RegCreateKeyEx(m_hKey, lpKeyName, 0, NULL, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &hKey, &dwOper);
	if(nRtn != ERROR_SUCCESS)
	{
		return nRtn;
	}
	nRtn = RegSetValueEx(hKey, subKey, 0, REG_DWORD, (BYTE *)&dwVal, sizeof(dwVal));

	RegCloseKey(hKey);
	return nRtn;
}

int Regedit::Read(LPCTSTR lpKeyName, CString* lpVal)
{
	int nRtn = ERROR_SUCCESS;	
	long dwSize=256;
	TCHAR szString[1024];
	HKEY hKey;
	nRtn = RegOpenKeyEx(m_hKey, lpKeyName, 0, KEY_ALL_ACCESS, &hKey);
	if(nRtn==ERROR_SUCCESS)
	{
		nRtn=RegQueryValue(hKey,NULL,szString,&dwSize);
	}
	if(nRtn==ERROR_SUCCESS)
	{
		*lpVal=szString;
	}
	RegCloseKey(hKey);
	return nRtn;
}

int Regedit::Read(LPCTSTR lpKeyName, LPCTSTR subKey, CString* lpVal)
{
	int nRtn = ERROR_SUCCESS;	
	DWORD dwSize=256;
	DWORD dwType = REG_SZ;
	TCHAR szString[1024];
	HKEY hKey;
	nRtn = RegOpenKeyEx(m_hKey, lpKeyName, 0, KEY_ALL_ACCESS, &hKey);
	if(nRtn==ERROR_SUCCESS)
	{
		nRtn=RegQueryValueEx(hKey,subKey,0, &dwType, (BYTE *)szString, &dwSize);
	}
	if(nRtn==ERROR_SUCCESS)
	{
		*lpVal=szString;
	}
	RegCloseKey(hKey);
	return nRtn;
}

int Regedit::Read(LPCTSTR lpKeyName, LPCTSTR subKey, DWORD* pdwVal)
{
	int nRtn = ERROR_SUCCESS;	
	DWORD dwSize=256;
	DWORD dwType = REG_SZ;
	DWORD szString;
	HKEY hKey;
	nRtn = RegOpenKeyEx(m_hKey, lpKeyName, 0, KEY_ALL_ACCESS, &hKey);
	if(nRtn==ERROR_SUCCESS)
	{
		nRtn=RegQueryValueEx(hKey,subKey,0, &dwType, (BYTE *)&szString,&dwSize);
	}
	if(nRtn==ERROR_SUCCESS)
	{
		*pdwVal=szString;
	}
	RegCloseKey(hKey);
	return nRtn;
}

int Regedit::Write(LPCTSTR lpKeyName, LPCTSTR lpVal, int len)
{
	int nRtn = ERROR_SUCCESS;
	HKEY hKey;
	nRtn = RegOpenKeyEx(m_hKey, lpKeyName, 0, KEY_ALL_ACCESS, &hKey);
	if(nRtn != ERROR_SUCCESS)
	{
		return nRtn;
	}
	nRtn = RegSetValueEx(hKey, NULL, 0, REG_SZ, (BYTE *)lpVal, len);

	RegCloseKey(hKey);
	return nRtn;
}

int Regedit::Write(LPCTSTR lpKeyName, DWORD dwVal)
{
	int nRtn = ERROR_SUCCESS;
	HKEY hKey;
	nRtn = RegOpenKeyEx(m_hKey, lpKeyName, 0, KEY_ALL_ACCESS, &hKey);
	if(nRtn != ERROR_SUCCESS)
	{
		return nRtn;
	}
	nRtn = RegSetValueEx(hKey, NULL, 0, REG_DWORD, (BYTE *)&dwVal, sizeof(dwVal));

	RegCloseKey(hKey);
	return nRtn;
}

int Regedit::Write(LPCTSTR lpKeyName, LPCTSTR subKey, LPCTSTR lpVal, int len)
{
	int nRtn = ERROR_SUCCESS;
	HKEY hKey;
	nRtn = RegOpenKeyEx(m_hKey, lpKeyName, 0, KEY_ALL_ACCESS, &hKey);
	if(nRtn != ERROR_SUCCESS)
	{
		return nRtn;
	}
	nRtn = RegSetValueEx(hKey, subKey, 0, REG_SZ, (BYTE *)lpVal, len);

	RegCloseKey(hKey);
	return nRtn;
}

int Regedit::Write(LPCTSTR lpKeyName, LPCTSTR subKey, DWORD dwVal)
{
	int nRtn = ERROR_SUCCESS;
	HKEY hKey;
	nRtn = RegOpenKeyEx(m_hKey, lpKeyName, 0, KEY_ALL_ACCESS, &hKey);
	if(nRtn != ERROR_SUCCESS)
	{
		return nRtn;
	}
	nRtn = RegSetValueEx(hKey, subKey, 0, REG_DWORD, (BYTE *)&dwVal, sizeof(dwVal));

	RegCloseKey(hKey);
	return nRtn;
}

int Regedit::DeleteKey(LPCTSTR lpKeyName)
{
	return RegDeleteKey(m_hKey, lpKeyName);
}

int Regedit::Open(LPCTSTR lpKeyName)
{
	int nRtn = ERROR_SUCCESS;
	HKEY hKey;
	long lReturn=RegOpenKeyEx(m_hKey,lpKeyName,0L,KEY_ALL_ACCESS,&hKey);
	return nRtn;
}

int Regedit::ListAllItem(LPCTSTR lpKeyName)
{
	int nRtn = ERROR_SUCCESS;
	HKEY hKey;
	int nIndex;
	TCHAR subKeyValue[256] = {'\0'};
	DWORD strLen;

	DWORD keyNum;
	DWORD valueNum;
	nRtn = RegOpenKeyEx(m_hKey, lpKeyName, 0, KEY_ALL_ACCESS, &hKey);
	if(nRtn != ERROR_SUCCESS)
	{
		return nRtn;
	}
	nRtn = RegQueryInfoKey(hKey, NULL, NULL, 0, &keyNum, NULL, NULL, &valueNum, NULL, NULL, NULL, NULL);
	// list the sub values
	for (nIndex = 0; nIndex < valueNum; nIndex++)
	{
		nRtn = RegEnumValue(hKey, nIndex, subKeyValue, &strLen, 0, NULL, NULL, NULL);
	}

	// list the sub keys
	for (nIndex = 0; nIndex < keyNum; nIndex++)
	{
		nRtn = RegEnumKeyEx(hKey, nIndex, subKeyValue, &strLen, 0, NULL, NULL, NULL);
	}


	RegCloseKey(hKey);
	return nRtn;
}
void Regedit::Close()
{
	return;
}

//end of file
